A little look at the use of Jndi in Spring, a small example. There are a lot of imperfect places, later look slowly, then change it.
Spring MVC
JDBC Template
Maven
Jndi
Maven POM Configuration
Spring-context,
/testds (Virtual Address) ------> ing descriptor ------> JDBC/testds (actual address)B: JDBC/testds (actual address)From this structure, A is indeed easy to transplant.
Let's look at an example:Assume that you need to obtain datasource, for example, datasource = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/testds ");When you map resources in the configuration f
, we define a data source named Mysqlds, whose parameters include the JDBC URL, driver class name, username and password, and so on.
2, in the program to refer to the data source: Connection Conn=null; Try {context ctx=new initialcontext ();Object datasourceref=ctx.lookup ("Java:mysqlds"); Reference data source DataSource ds= (DataSource) datasourceref; Conn=d
application|web| Access
Webshpere Studio Application developer Jndi Access datasource The following error, please help the master!
On the server I set the DataSource is Jdbc/ds1
Get connection Failed.javax.naming.NamingException:The JNDI operation "Lookup" in the context localhost/nodes/ Localhost/servers/server1 "wi
1. Connection PoolThe acquisition and opening of connection is a costly process, and the solution to this problem is connection pooling.Connection pool is to obtain a certain number of connection in advance, the process of processing is not a new connection, but to obtain a pre-prepared connection right to use.2, DataSourceThe technique of providing connection pooling function is called DataSource:Javax.sql.DataSourceThe generic Web server is provided. Like Tomcat5.5.
Usually in J2EE development, datasource configured on the server through JNDI is used, for example, the final application runs on WebLogic, while Tomcat may be used on the local machine in the development environment, at this time, we need to configure a datasource on Tomcat. on the Internet, we can see that many of the solutions are to change the server in the T
. Hikaridatasource is the default choice for spring boot (datasourcebuilder in data_source_type_names[0]= Com.zaxxer.hikari.HikariDataSource). Therefore, when the following configuration is done in the Application.yml file, Spring boot uses the Hikaridatasource database connection pool by default.Spring: datasource: URL: Jdbc:mysql://localhost:3306/sas use
Basic Principles of database connection pool technology:
A data source object is provided by a Web Container (such as Tomcat ).ProgramUsing the JNDI technology to obtain this object. Generally, If you create a connection by yourself, use the followingCodeGet the data source object:
Context context = new initialcontext ();
Datasource = (datasource) Context.
the bin type, in the process of analysis, the use of Windows computer opened, direct error, after careful review, suspected memory overflow, directly into the Mac opened, then waited 10 minutes to open. Specific operation to my detailed collation and then provide reference. When you navigate to the problem, you find that the number of configured links is too large. The default database has been unable to eat so many links. The configuration at that time was: From this summary, the test environ
There are three methods to obtain the DataSource object in the Spring framework:1. Obtain DataSource from JNDI.2. Obtain DataSource from a third-party connection pool.3. Use DriverManagerDataSource to obtain the DataSource.
1. Obt
1. Use org. springframework. JDBC. datasource. drivermanagerdatasource
Note: When drivermanagerdatasource establishes a connection, a new connection is created as long as there is a connection, and there is no function of the connection pool.
2. Use org. Apache. commons. DBCP. basicdatasourceNote: This is a recommended data source configuration method. It actually uses the connection pool technology.3. Use org. springframework.
There are 3 ways to get DataSource objects in the Spring framework:
1. Obtain DataSource from Jndi.
2. Obtain DataSource from a third party connection pool.
3. Use Drivermanagerdatasource to obtain datasource.
First, get
application is configured on a high-performance application server, such as WebLogic or WebSphere, we may prefer to use the data source provided by the application server itself. The data source for the application server is used by Jndi open callers, and spring specifically provides the Jndiobjectfactorybean class that references the Jndi resource. The followin
DataSource can centrally manage database connections, reduce maintenance effort, and make deployment easier;Spring DataSource configuration: (Spring data source configuration) use dbcp here, there are very many other such as C3p0,jdbc,jndi, etc.DataSource, like defining ordi
/spring-datasource-mogon.xml] cannot be opened because it does not existAt org. springframework. core. io. ClassPathResource. getInputStream (ClassPathResource. java: 157)At org. springframework. beans. factory. xml. XmlBeanDefinitionReader. loadBeanDefinitions (XmlBeanDefinitionReader. java: 328)... 26 morez
The code for loading the configuration file is as follows:
ApplicationContext context = new ClassPa
/test
Spring.datasource.username=dbuser
Spring.datasource.password=dbpass
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Other optional configurations allow you to view the datasourceproperties. Also note that you can configure any DataSource related specific attributes through Spring.datasource.*: Refer specifically to the connection pool implementation document you are using.
Note: Since
A generic data source is used within the project. Consider that there is no distributed data access. The APACHE-COMMONS-DBCP was used. However, a third party report making tool was added to the project. The tool forces the use of Jndi to obtain a connection (damn) There's no way. It's only added. The configuration steps and an error note are shown below:
1. Environment: Windows XP system. The jbossGA-4.2.2 sqlserver2000 is used.
The project architec
two in spring boot DataSource . Correspondingly, we will create two JdbcTemplate beans, each using these two data sources.First, we must application.yml declare the configuration of two data sources in, one using spring.datasource , the other using spring.second-datasource :SpringApplication:Namedata-MultidatasourceDataSourcedriver-class-
Spring configuration JNDI Solution
My configuration environment is: Spring + Tomcat + MySQL
Note:
1. $ TOMCAT_HOME indicates the Tomcat installation directory.
Step 1: configure the data source in the $ TOMCAT_HOME/conf/context. xml file of Tomcat.
Name = "jdbc/TestDB"Auth = "Container"Type = "javax. SQL.
implements Beanclassloaderaware, Initializingbean {private ClassLoader ClassLoader; /** * Name of the DataSource. The Default to "TestDB" is when using a embedded database. */private String name; /** * Whether to generate a random datasource name. */Private
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.